package sjq.com.radar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import java.util.List;

/**
 * Created by chengengsen on 2016/8/10.
 */
public class RadarView extends View{

    /** 绘制多边形的画笔 */
    private Paint mPaintPolygon;
    /** 绘制直线的画笔 */
    private Paint mPaintLine;
    /** 绘制分值区的画笔 */
    private Paint mPaintRegion;
    /** 绘制文字的画笔 */
    private Paint mPainText;


    /** 中心点 x 轴坐标*/
    private int mCenterX;
    /** 中心点 y 轴坐标 */
    private int mCenterY;

    /** 标题(基本素质、职业素质等)*/
    private List<String> title;
    /** 测评要素*/
    private List<String> assNum;
    /** 分数*/
    private List<Integer> score;
    /** 多边形的个数 */
    private int num;
    /**颜色 */
    private List<String> colors;


    /** 角度 */
    private float  mAngel ;

    /** 多边形的绘制路径 */
    private Path mPathPolygon;

    /** 多边形的起始半径 */
    private float mStartRadius = 20F;

    /**
     * 直线的绘制路径
     */
    private Path mPathLine;

    /**
     * 区域的绘制路径
     */
    private Path mPathRegion;



    private float radius ;

    public RadarView(Context context) {
        this(context, null);
    }

    public RadarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {


        mPaintPolygon = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaintPolygon.setStyle(Paint.Style.STROKE);
        mPaintPolygon.setColor(Color.parseColor("#dddddd"));
        mPaintPolygon.setStrokeWidth(2F);

        mPaintLine = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaintLine.setStyle(Paint.Style.STROKE);
        mPaintLine.setColor(Color.parseColor("#cccccc"));
        mPaintLine.setStrokeWidth(1F);

        mPaintRegion = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaintRegion.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaintRegion.setStrokeWidth(1F);

        mPainText = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPainText.setStyle(Paint.Style.FILL);
        mPainText.setStrokeWidth(1F);
        mPainText.setColor(Color.WHITE);

        mPathPolygon = new Path();
        mPathLine = new Path();
    }

    protected void onSizeChanged (int w, int h, int oldw, int oldh) {
        mCenterX = w / 2;
        mCenterY = h / 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawPolygon(canvas);
        drawLine(canvas);
        drawRegion(canvas);
        drawText(canvas);
    }

    /**
     * 绘制多边形
     */
    private void drawPolygon(Canvas canvas) {
        radius = mStartRadius * num;
        mAngel = (float) (2 * Math.PI / score.size());
        for(int i = 1; i <= num; i++){
            float newRadius = mStartRadius * i;
            mPathPolygon.moveTo(mCenterX , mCenterY - newRadius);

            for (int j = 1; j < score.size(); j++) {
                float x = (float) (mCenterX - Math.sin(mAngel * j) * newRadius);
                float y = (float) (mCenterY - Math.cos(mAngel * j) * newRadius);
                mPathPolygon.lineTo(x, y);
            }

            mPathPolygon.close();  // 与起始点连接，形成封闭路径
            canvas.drawPath(mPathPolygon, mPaintPolygon);
            mPathPolygon.reset();
        }
    }

    /**
     * 绘制直线
     */
    private void drawLine(Canvas canvas) {
        radius = mStartRadius * num;
        mAngel = (float) (2 * Math.PI / score.size());
        float radius = mStartRadius * num;
        for (int i = 0; i < score.size(); i++) {
            mPathLine.moveTo(mCenterX, mCenterY);

            float x = (float) (mCenterX - Math.sin(mAngel * i) * radius);
            float y = (float) (mCenterY - Math.cos(mAngel * i) * radius);

            mPathLine.lineTo(x, y);

            canvas.drawPath(mPathLine, mPaintLine);
        }
    }

    private float mX, mY;              // 相邻两个点的中心点横坐标和纵坐标
    private float mX1, mY1;          //第一个点的横坐标和纵坐标
    private float mX2, mY2;          //第一个点和最后一个点的中心点横坐标和纵坐标
    /**
     * 绘制分值区
     */
    private void drawRegion(Canvas canvas) {
        radius = mStartRadius * num;
        mAngel = (float) (2 * Math.PI / score.size());

        for (int i = 1; i <= score.size(); i++) {
            float x , y ;
            float x1 = 0, y1 = 0;
            x = (float) (Math.sin(mAngel * (i-1)) * radius * ((float)(score.get(i - 1)) / 100f));
            y = (float) (Math.cos(mAngel * (i-1)) * radius * ((float)(score.get(i - 1)) / 100f));
            if(i < score.size()){
                x1 = (float) (Math.sin(mAngel * i) * radius * ((float)(score.get(i )) / 100f));
                y1 = (float) (Math.cos(mAngel * i) * radius * ((float)(score.get(i)) / 100f));
            }

            mX2 = ( (float) (Math.sin(mAngel * (0)) * radius * ((float)(score.get(0)) / 100f)) + (float) (Math.sin(mAngel * (score.size()-1)) * radius * ((float)(score.get(score.size()-1)) / 100f))) / 2;
            mY2 = ( (float) (Math.cos(mAngel * (0)) * radius * ((float)(score.get(0)) / 100f)) + (float) (Math.cos(mAngel * (score.size()-1)) * radius * ((float)(score.get(score.size() - 1)) / 100f))) / 2 ;

            if (i == 1) {
                mPathRegion = new Path();
                mPaintRegion.setColor(Color.parseColor(colors.get(i-1)));
                mPathRegion.moveTo(mCenterX , mCenterY );
                mPathRegion.lineTo(mCenterX + mX2, mCenterY - mY2);
                mPathRegion.lineTo(mCenterX + x, mCenterY - y);
                mPathRegion.lineTo(mCenterX + (x+x1)/2, mCenterY - (y+y1)/2);
                mPathRegion.close();
                mX = (x+x1)/2;
                mY = (y+y1)/2;
                mX1 =  x;
                mY1 =  y;
            } else if(i == score.size()-1){
                mPathRegion = new Path();
                mPaintRegion.setColor(Color.parseColor(colors.get(i-1)));
                mPathRegion.moveTo(mCenterX , mCenterY );
                mPathRegion.lineTo(mCenterX + mX, mCenterY - mY);
                mPathRegion.lineTo(mCenterX + x, mCenterY - y);
                mPathRegion.lineTo(mCenterX + (x+x1)/2, mCenterY - (y+y1)/2);
                mPathRegion.close();
                mX = (x+x1)/2;
                mY = (y+y1)/2;
            }else if(i == score.size()){
                mPathRegion = new Path();
                mPaintRegion.setColor(Color.parseColor(colors.get(i-1)));
                mPathRegion.moveTo(mCenterX , mCenterY );
                mPathRegion.lineTo(mCenterX + mX, mCenterY - mY);
                mPathRegion.lineTo(mCenterX + x, mCenterY - y);
                mPathRegion.lineTo(mCenterX + (x+mX1)/2, mCenterY - (y+mY1)/2);
                mPathRegion.close();
            } else {
                mPathRegion = new Path();
                mPaintRegion.setColor(Color.parseColor(colors.get(i-1)));
                mPathRegion.moveTo(mCenterX , mCenterY );
                mPathRegion.lineTo(mCenterX + mX, mCenterY - mY);
                mPathRegion.lineTo(mCenterX + x, mCenterY - y);
                mPathRegion.lineTo(mCenterX + (x+x1)/2, mCenterY - (y+y1)/2);
                mPathRegion.close();
                mX = (x+x1)/2;
                mY = (y+y1)/2;
            }


            canvas.drawPath(mPathRegion, mPaintRegion);
        }
        mPathRegion.close();
    }

    private void drawText(Canvas canvas){
        radius = mStartRadius * num;
        mAngel = (float) (2 * Math.PI / score.size());
        mPaintRegion.setTextSize(2.8f * num);
        mPainText.setTextSize(2.4f * num);

        for (int i = 0; i < score.size(); i++) {
            float x , y;
            float x1, y1 ;
            x = (float) (Math.sin(mAngel * i) * radius * ((float)(score.get(i)) / 100f));
            y = (float) (Math.cos(mAngel * i) * radius * ((float)(score.get(i)) / 100f));
            x1 = (float) (Math.sin(mAngel * i) * radius );
            y1    = (float) (Math.cos(mAngel * i) * radius );
            if (i == 0) {
                mPaintRegion.setColor(Color.parseColor(colors.get(i)));
                canvas.drawText(""+score.get(i),mCenterX - x -15 , mCenterY - y + 4*num, mPainText);
                canvas.drawText(assNum.get(i), mCenterX - (getTextWidth(mPaintRegion, assNum.get(i))) / 2, mCenterY - y1 - (int)(1.5 * num) ,mPaintRegion);
                canvas.drawText(title.get(i),mCenterX - (getTextWidth(mPaintRegion, title.get(i))) / 2, mCenterY - y1 - 5 * num ,mPaintRegion);
            } else if(i == 1){
                mPaintRegion.setColor(Color.parseColor(colors.get(i)));
                canvas.drawText(""+score.get(i), mCenterX + x - 40 , mCenterY - y + 20, mPainText);
                canvas.drawText(assNum.get(i), mCenterX + x1 + 10, mCenterY - y1 ,mPaintRegion);
                canvas.drawText(title.get(i), mCenterX + x1 + 30, mCenterY - y1 - 40 ,mPaintRegion);
            }else if(i == 2){
                mPaintRegion.setColor(Color.parseColor(colors.get(i)));
                canvas.drawText(""+score.get(i), mCenterX + x - 40 , mCenterY - y - 10, mPainText);
                canvas.drawText(assNum.get(i), mCenterX + x1 + 1 * num, mCenterY - y1 + 4 * num,mPaintRegion);
                canvas.drawText(title.get(i), mCenterX + x1 + 1 * num, mCenterY - y1  ,mPaintRegion);
            } else if(i == 3){
                mPaintRegion.setColor(Color.parseColor(colors.get(i)));
                canvas.drawText(""+score.get(i), mCenterX + x -15 , mCenterY - y - 20, mPainText);
                canvas.drawText(assNum.get(i), mCenterX - (getTextWidth(mPaintRegion, assNum.get(i))) / 2, mCenterY - y1 + 8 * num ,mPaintRegion);
                canvas.drawText(title.get(i), mCenterX - (getTextWidth(mPaintRegion, title.get(i))) / 2, mCenterY - y1 + 4 * num ,mPaintRegion);
            } else if(i == 4){
                mPaintRegion.setColor(Color.parseColor(colors.get(i)));
                canvas.drawText(""+score.get(i), mCenterX + x + 10 , mCenterY - y , mPainText);
                canvas.drawText(assNum.get(i), mCenterX + x1- 10 - (getTextWidth(mPaintRegion, title.get(i))), mCenterY - y1 + 4 * num  ,mPaintRegion);
                canvas.drawText(title.get(i), mCenterX + x1 - 10 - (getTextWidth(mPaintRegion, title.get(i))), mCenterY - y1 ,mPaintRegion);
            } else{
                mPaintRegion.setColor(Color.parseColor(colors.get(i)));
                canvas.drawText(""+score.get(i), mCenterX + x + 20 , mCenterY - y + 20, mPainText);
                canvas.drawText(assNum.get(i), mCenterX + x1 - 10 - (getTextWidth(mPaintRegion, title.get(i))), mCenterY - y - 2 *num  ,mPaintRegion);
                canvas.drawText(title.get(i), mCenterX + x1 - 10 - (getTextWidth(mPaintRegion, title.get(i))), mCenterY - y - 6 * num ,mPaintRegion);

            }

        }
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public List<Integer> getScore() {
        return score;
    }

    public void setScore(List<Integer> score) {
        this.score = score;
    }

    public List<String> getTitle() {
        return title;
    }

    public void setTitle(List<String> title) {
        this.title = title;
    }

    public List<String> getAssNum() {
        return assNum;
    }

    public void setAssNum(List<String> assNum) {
        this.assNum = assNum;
    }

    public List<String> getColors() {
        return colors;
    }

    public void setColors(List<String> colors) {
        this.colors = colors;
    }

    /**
     * 获取文本宽度
     * @param paint
     * @param str
     * @return
     */
    public static int getTextWidth(Paint paint, String str) {
        int iRet = 0;
        if (str != null && str.length() > 0) {
            int len = str.length();
            float[] widths = new float[len];
            paint.getTextWidths(str, widths);
            for (int j = 0; j < len; j++) {
                iRet += (int) Math.ceil(widths[j]);
            }
        }
        return iRet;
    }




}
